package cn.schoolwow.quickdao.module.database.ddl.query.flow.property.getEntityProperty;

import cn.schoolwow.quickdao.annotation.IdStrategy;
import cn.schoolwow.quickdao.domain.entity.Entity;
import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetEntityPropertyBySQLiteFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        setPropertyInfo(flowContext);
        setPropertyForeignKey(flowContext);
    }

    @Override
    public String name() {
        return "获取数据库表指定字段-SQLite数据库";
    }

    private void setPropertyInfo(FlowContext flowContext){
        Entity entity = (Entity) flowContext.checkData("entity");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取表指定字段信息")
                .putTemporaryData("sql", "PRAGMA table_info(`" + entity.tableName + "`);")
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while (resultSet.next()) {
                            Property property = new Property();
                            property.column = resultSet.getString("name");
                            property.columnType = resultSet.getString("type");
                            property.notNull = "1".equals(resultSet.getString("notnull"));
                            if (null != resultSet.getString("dflt_value")) {
                                property.defaultValue = resultSet.getString("dflt_value");
                            }
                            if (1 == resultSet.getInt("pk")) {
                                property.id = true;
                                property.strategy = IdStrategy.AutoIncrement;
                            }
                            entity.properties.add(property);
                        }
                    }
                })
                .execute();
    }

    private void setPropertyForeignKey(FlowContext flowContext){
        Entity entity = (Entity) flowContext.checkData("entity");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取单个表字段外键信息")
                .putTemporaryData("sql", "select name,sql from sqlite_master where type='table' and name = ?;")
                .putTemporaryData("parameters", Arrays.asList(entity.tableName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        while (resultSet.next()) {
                            if(!entity.tableName.equalsIgnoreCase(resultSet.getString("name"))){
                                continue;
                            }
                            String sql = resultSet.getString("sql");
                            for(Property property:entity.properties){
                                if(sql.contains("foreign key(\""+property.column+"\")")){
                                    property.foreignKeyColumn = true;
                                }
                            }
                        }
                    }
                })
                .execute();
    }
}
